package com.cool.collect.demo;

// 抽象策略角色
interface CalculateStrategy {
    int calculate(int a, int b);
}

// 具体策略角色：加法
class AddStrategy implements CalculateStrategy {
    @Override
    public int calculate(int a, int b) {
        return a + b;
    }
}

// 具体策略角色：减法
class SubtractStrategy implements CalculateStrategy {
    @Override
    public int calculate(int a, int b) {
        return a - b;
    }
}

// 具体策略角色：乘法
class MultiplyStrategy implements CalculateStrategy {
    @Override
    public int calculate(int a, int b) {
        return a * b;
    }
}

// 环境角色
class Calculator {
    private CalculateStrategy calculateStrategy;

    public Calculator(CalculateStrategy calculateStrategy) {
        this.calculateStrategy = calculateStrategy;
    }

    public int calculate(int a, int b) {
        return calculateStrategy.calculate(a, b);
    }
}

// 测试
public class StrategyPatternDemo {
    public static void main(String[] args) {
        Calculator calculator = new Calculator(new AddStrategy());
        int result = calculator.calculate(3, 4);
        System.out.println(result); // 输出 7
        calculator = new Calculator(new SubtractStrategy());
        result = calculator.calculate(5, 2);
        System.out.println(result); // 输出 3
        calculator = new Calculator(new MultiplyStrategy());
        result = calculator.calculate(2, 6);
        System.out.println(result); // 输出 12
    }
}